package StudyCode.summary.myfile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class GetFileSHA384 {
	public static void main(String[] args) {
		File file = new File("E:\\迅雷下载\\test1.txt");
		System.out.println("文件 " + file + " SHA384值是:");
		System.out.println(getFileSHA1(file));
	}

	private static String getFileSHA1(File file) {
		String str = "";
		try {
			str = getHash(file, "SHA-384");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return str;
	}

	private static String getHash(File file, String hashType) throws Exception {
		InputStream fis = new FileInputStream(file);
		byte buffer[] = new byte[1024];
		MessageDigest md5 = MessageDigest.getInstance(hashType);
		for (int numRead = 0; (numRead = fis.read(buffer)) > 0; ) {
			md5.update(buffer, 0, numRead);
		}

		fis.close();
		return toHexString(md5.digest());
	}

	private static String toHexString(byte b[]) {
		StringBuilder sb = new StringBuilder();
		for (byte aB : b) {
			sb.append(Integer.toHexString(aB & 0xFF));
		}
		return sb.toString();
	}
}
